package ms07.ticketPro;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import android.database.Cursor;

public class Ticket implements Serializable {

	private static final long serialVersionUID = 2098024840084394838L;
	
	protected long internal_id;
	protected int id;
	protected String organisation;
	protected double price;
	protected Date valid_from;
	protected Date valid_till;
	protected String city_from;
	protected String city_to;
	protected int adults;
	protected int children;
	protected String memberCard;
	protected String seatclass;
	protected String username;
	
	protected static HashMap<Long,Ticket> instances;
	
	protected Vector<String> validation;
	
	public static Ticket getTicketInstance(long internal_id){
		if(instances==null) instances = new HashMap<Long,Ticket>();
		
		Ticket ticket = instances.get(internal_id);
		
		if(ticket!=null) return ticket;
		
		return null;
	}
	
	public static Ticket getTicketInstance(Cursor cursor){
		Ticket ticket = getTicketInstance(cursor.getLong(0));
		
		if(ticket!=null) return ticket;
		
		ticket = new Ticket();
		
		instances.put(cursor.getLong(0), ticket);
		
		int i=0;
		ticket.setInternalId(cursor.getLong(i++));
		ticket.setId(cursor.getInt(i++));
		ticket.setOrganisation(cursor.getString(i++));
		ticket.setPrice(cursor.getDouble(i++));
		Date valid_from = new Date();
		valid_from.setTime(cursor.getLong(i++));
		ticket.setValid_from(valid_from);
		Date valid_till = new Date();
		valid_till.setTime(cursor.getLong(i++));
		ticket.setValid_till(valid_till);
		ticket.setCity_from(cursor.getString(i++));
		ticket.setCity_to(cursor.getString(i++));
		ticket.setAdults(cursor.getInt(i++));
		ticket.setChildren(cursor.getInt(i++));
		ticket.setMemberCard(cursor.getString(i++));
		ticket.setSeatclass(cursor.getString(i++));
		ticket.setUsername(cursor.getString(i++));
		String[] validation = cursor.getString(i++).split(",");
		for(int k=0; k<validation.length; k++){
			ticket.addValidation(validation[k]);
		}
		
		return ticket;
	}
	
	public Ticket(){
		
	}

	/**
	 * save this ticket to the database!
	 */
	public long save(){
		Database database = MyApplication.getInstance().getDatabase();
		this.internal_id = database.insertTicket(this);
		return this.getInternalId();
	}
	
	public boolean delete(){
		Database database = MyApplication.getInstance().getDatabase();
		return database.deleteTicket(this);
	}

	public void setInternalId(long internal_id) {
		DebugLog.getInstance().log("Ticket::serInternalId("+internal_id+")");
		this.internal_id = internal_id;
	}
	
	public long getInternalId(){
		return internal_id;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		DebugLog.getInstance().log("Ticket::setId("+id+")");
		this.id = id;
	}

	public String getOrganisation() {
		return organisation;
	}

	public void setOrganisation(String organisation) {
		DebugLog.getInstance().log("Ticket::setOrganisation("+organisation+")");
		this.organisation = organisation;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		DebugLog.getInstance().log("Ticket::setPrice("+price+")");
		this.price = price;
	}

	public Date getValid_from() {
		return valid_from;
	}

	public void setValid_from(Date valid_from) {
		DebugLog.getInstance().log("Ticket::setValid_from("+(valid_from==null?"NULL":valid_from.getTime())+")");
		this.valid_from = valid_from;
	}

	public Date getValid_till() {
		return valid_till;
	}

	public void setValid_till(Date valid_till) {
		DebugLog.getInstance().log("Ticket::setValid_till("+(valid_till==null?"NULL":valid_till.getTime())+")");
		this.valid_till = valid_till;
	}

	public String getCity_from() {
		return city_from;
	}

	public void setCity_from(String city_from) {
		DebugLog.getInstance().log("Ticket::setCity_from("+city_from+")");
		this.city_from = city_from;
	}

	public String getCity_to() {
		return city_to;
	}

	public void setCity_to(String city_to) {
		DebugLog.getInstance().log("Ticket::setCity_to("+city_to+")");
		this.city_to = city_to;
	}

	public int getAdults() {
		return adults;
	}

	public void setAdults(int adults) {
		DebugLog.getInstance().log("Ticket::setAdults("+adults+")");
		this.adults = adults;
	}

	public int getChildren() {
		return children;
	}

	public void setChildren(int children) {
		DebugLog.getInstance().log("Ticket::setChildren("+children+")");
		this.children = children;
	}

	public String getMemberCard() {
		return memberCard;
	}

	public void setMemberCard(String memberCard) {
		DebugLog.getInstance().log("Ticket::setMemberCard("+memberCard+")");
		this.memberCard = memberCard;
	}

	public String getSeatclass() {
		return seatclass;
	}

	public void setSeatclass(String seatclass) {
		DebugLog.getInstance().log("Ticket::setSeatclass("+seatclass+")");
		this.seatclass = seatclass;
	}

	public Vector<String> getValidation() {
		return validation;
	}

	public void setValidation(Vector<String> validation) {
		this.validation = validation;
	}
	
	public void addValidation(String validation){
		DebugLog.getInstance().log("Ticket::addValidation("+validation+")");
		if(this.validation == null){
			this.validation = new Vector<String>();
		}
		
		this.validation.add(validation);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		DebugLog.getInstance().log("Ticket::setUsername("+username+")");
		this.username = username;
	}
}
